import supplierApi from "../services/supplier"
export default {
    namespaced: true,
    state: {
        data: [],  //供应商数据
        total:0,   //总条数
        condition: {  //模糊分页
            page: 1,
            limit: 3,
            key: "",
            field: "",
            sort: ""
        },
        editSupplier:{} //当前正在修改
    },
    mutations: {
        mgetSupplier(state, supplier) { //所有供应商
            state.data = supplier;
        },
        settotal(state,total){ //设置总条数
            state.total = total;
        },
        setCondition(state, condition) {  //模糊查询分页
            state.condition = {
                ...state.condition,
                ...condition
            }
        },
        setEditSupplier(state, user) {    //设置正在修改
            state.editSupplier = user
        },
        deleteSupplier(state, id) {  //删除
            state.data = state.data.filter(it => it._id !== id)
        }
    },
    actions: {
        async agetSupplier(context) {
            const data = await supplierApi.getSupplier(context.state.condition);
            context.commit("mgetSupplier", data.data); //触发事件改变
            context.commit("settotal",data.count)
        },
        async addSupplier(context, user) {
            await supplierApi.addSupplier(user)
        },
        async deleteSupplier(context, id) {
            await supplierApi.deleteSupplier(id);
            context.commit("deleteSupplier", id)
        },
        async setEditSupplier(context, id) {
            const data = await supplierApi.getSupplierById(id);
            context.commit("setEditSupplier",data.data);
        },
        async modifySupplier(context, s) {
            await supplierApi.modifySupplier(s)
        }
    }
}